/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ntb.controllers;

import com.ntb.common.StringUtil;
import com.ntb.controllers.forms.LoginForm;
import com.ntb.models.UsersDAO;
import com.ntb.models.impl.UsersDAOImpl;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Thinh Hung
 */
public class LoginAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) {
        LoginForm loginForm = (LoginForm) form;

        String action = loginForm.getAction();

        ActionForward forward = mapping.getInputForward();
        
        if (request.getSession().getAttribute("userid") != null) {
            forward = mapping.findForward("dashboard");
        }
        else if (("login").equalsIgnoreCase(action)) {
            UsersDAO usersDAO = new UsersDAOImpl();
            StringUtil stringUtil = new StringUtil();
            String username = "";
            String password = "";
            String remember = "";
            try {
                username = loginForm.getUsername().trim().toLowerCase();
                password = stringUtil.generateMD5(loginForm.getPassword());
                remember = loginForm.getRemember();
            } catch (NullPointerException e) {
            }

            int userId = usersDAO.login(username, password);
            if (userId != -1) {
                if ("yes".equals(remember)) {
                    int time = 2592000; // 30days

                    // Set remember cookie
                    Cookie cookie = new Cookie("userid", String.valueOf(userId));
                    cookie.setMaxAge(time);
                    response.addCookie(cookie);

                    String rememberCode = stringUtil.generateRandomString();
                    cookie = new Cookie("remembercode", rememberCode);
                    cookie.setMaxAge(time);
                    response.addCookie(cookie);

                    usersDAO.updateRememberCode(stringUtil.generateMD5(rememberCode), userId);
                } else {
                    // Delete all remember cookie
                    Cookie cookie = new Cookie("userid", "");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);

                    cookie = new Cookie("remembercode", "");
                    cookie.setMaxAge(0);
                    response.addCookie(cookie);
                    usersDAO.updateRememberCode("", userId);
                }
                request.getSession(true).setAttribute("userid", userId);

                return mapping.findForward("dashboard");
            } else {
                request.setAttribute("errorMessage", "Invalid username and/or password!");
            }
        }
        return forward;
    }
}
